<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="优惠券名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入优惠券名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="优惠券类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="coupon_type" placeholder="请选择优惠券类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="优惠券金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input-number v-model="model.amount" placeholder="请输入优惠券金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="最低消费金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="minOrderAmount">
              <a-input-number v-model="model.minOrderAmount" placeholder="请输入最低消费金额门槛" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="生效日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startDate">
              <j-date placeholder="请选择优惠券生效开始日期"  v-model="model.startDate" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="失效日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
              <j-date placeholder="请选择优惠券失效日期"  v-model="model.endDate" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="发行总量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalQuantity">
              <a-input-number v-model="model.totalQuantity" @change='valueChange' placeholder="请输入发行总量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="剩余数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remainingQuantity" >
              <a-input-number v-model="model.remainingQuantity" placeholder=""  disabled='true' style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="已使用数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="usedQuantity" >
              <a-input-number v-model="model.usedQuantity" placeholder="" disabled='true' style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单用户限领数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maxPerUser">
              <a-input-number v-model="model.maxPerUser" placeholder="请输入单用户限领数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="适用商品分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="targetCategoryName">
              <j-popup
                v-model="model.targetCategoryName"
                field="targetCategoryName"
                org-fields="id,name"
                dest-fields="targetCategoryId,targetCategoryName"
                code="SelectorGoodsCategory"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="适用商品" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="targetProductName">
              <j-popup
                v-model="model.targetProductName"
                field="targetProductName"
                org-fields="id,name"
                dest-fields="targetProductId,targetProductName"
                code="SelectorGoods"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="优惠券状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status" v-show='false'>
              <j-dict-select-tag type="list" v-model="model.status" dictCode="coupon_status" placeholder="请选择优惠券状态" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MallCouponForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          code: [
            { required: true, message: '请输入优惠券名称!'},
          ],
          type: [
            { required: true, message: '请选择优惠券类型'}
          ]
        },
        url: {
          add: "/coupon/mallCoupon/add",
          edit: "/coupon/mallCoupon/edit",
          queryById: "/coupon/mallCoupon/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },

      valueChange(newValue){
        this.model.remainingQuantity = newValue;
        this.model.usedQuantity = 0;
      }
    }
  }
</script>